const fp = require('lodash/fp');
const {Maybe, Container} = require('./support');

let maybe = Maybe.of([5, 6, 1])
let ex1 = (x) => {
  // 传入想要增加值的量 +1、 +2
  // to something
  return maybe.map(value => fp.map(fp.add(x), value))
}
console.log('值增加1', ex1(1));
console.log('值增加3', ex1(3));

// 练习2
let xs = Container.of(['do1', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do'])
let ex2 = xs.map(fp.first)
  // console.log(xs.map(fp.first));
console.log('练习2', ex2); 

// 练习3
let safeProp = fp.curry(function (x, o) {
  return Maybe.of(o[x])
})
let user = {id: 2, name: 'Albert'}
let ex3 = fp.flowRight(fp.map(fp.first), safeProp('name'));
  // console.log(fp.first(safeProp('name', user)));
console.log('练习3', ex3(user));


// 练习4
// 重写前
// let ex4 = function (n) {
//   if (n) {
//     return parseInt(n)
//   }
// }

// 重写后
let ex4 = function (n) {
  return Maybe.of(n).map(parseInt)
}
console.log(ex4(3.2));
console.log(ex4('sss4.5'));